﻿@using DCMS.Web.Extensions;
@using DCMS.Web.Framework;
@using DCMS.Web.Framework.UI;
@using DCMS.ViewModel.Models.Finances;
@using DCMS.ViewModel.Models.Report;
@using System.Collections.Generic;
@using System.Linq;
@using DCMS.Core.Domain.Common;

@model BusinessUserVisitOfYearListModel

<section id="content_wrapper">

    <!-- Start: Topbar-Dropdown -->
    <div id="topbar-dropmenu">
        @await Html.PartialAsync("_ToolBox")
    </div>
    <!-- End: Topbar-Dropdown -->
    <!-- Start: Topbar -->
    <header id="topbar">
        <div class="topbar-left">
            <ol class="breadcrumb">
                <li class="crumb-active">
                    <a href="#">报表</a>
                </li>
                <li class="crumb-icon">
                    <a href="@Url.RouteUrl("HomePage")">
                        <span class="glyphicon glyphicon-home"></span>
                    </a>
                </li>
                <li class="crumb-link">
                    <a href="#">员工报表</a>
                </li>
                <li class="crumb-trail">业务员业绩</li>
                <li class="crumb-trail">每日拜访明细</li>
            </ol>
        </div>
        <div class="topbar-right">
            <div class="ml15 ib va-m" id="toggle_sidemenu_r">
                <a href="#" class="pl5">
                    <i class="fa fa-sign-in fs22 text-primary"></i>
                    <span class="badge badge-hero badge-danger">3</span>
                </a>
            </div>
        </div>
    </header>
    <!-- End: Topbar -->
    <!-- Begin: Content -->
    <section id="content" class="formSection">

        <div class="special-alerts">
            <div class="alert alert-danger light alert-dismissable" id="alert-demo-2" style="display: none;">
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                <i class="fa fa-info pr10"></i>
                <strong>您没有被授权此操作!</strong>
            </div>
        </div>

        <div class="row">
            <div class="col-md-12">

                <div class="panel">
                    <div class="panel-heading">
                        <div class="panel-title">
                            <span class="glyphicon glyphicon-tasks"></span> 业务员拜访明细报表
                        </div>
                    </div>
                    <div class="panel-body p10">
                        <div class="bootstrap-table">
                            <div class="fixed-table-toolbar">
                                <div class="bs-bars pull-left">
                                    <div class="col-sm-2" id="yearbox">
                                        <select class="form-control" id="year" name="year" style="width:120px;margin-right:500px;">
                                            @{
                                                List<int> Years = new List<int>();
                                                for (int i = 2021; i <= DateTime.Now.Year; i++)
                                                {
                                                    Years.Add(i);
                                                }
                                                foreach (var item in Years)
                                                {
                                                    if (item == Model.Year)
                                                    {
                                                        <option value="@(item)" selected="selected">@(item)</option>
                                                    }
                                                    else
                                                    {
                                                        <option value="@(item)">@(item)</option>
                                                    }
                                                }
                                            }
                                        </select>
                                    </div>
                                    <div class="col-sm-2" id="monthbox">
                                        <select class="form-control" id="month" name="month" style="width:100px;">
                                            @{
                                                var beginMonth = 1;
                                                var endMonth = Model.Month;
                                                if (Model.Year == 2021)
                                                {
                                                    beginMonth = 7;
                                                }
                                                for (int i = beginMonth; i <= endMonth; i++)
                                                {
                                                    if (i == Model.Month)
                                                    {
                                                        <option value="@(i)" selected="selected">@(i)</option>
                                                    }
                                                    else
                                                    {
                                                        <option value="@(i)">@(i)</option>
                                                    }
                                                }
                                            }
                                        </select>
                                    </div>
                                </div>
                            </div>

                            <div class="panel panel-defaulpanel1t" id="panel1">

                            </div>
                        </div>
                        <div class="clearfix"></div>
                    </div>
                </div>
            </div>
        </div>


    </section>

    <!-- End: Content -->
</section>

<div id="modal-image" class="popup-basic popup-lg mfp-hide">
    <img class="img-responsive" src="https://resources.jsdcms.com:8999/content/images/loading.gif">
</div>

@await Html.PartialAsync("_Windows", Model)

@*@{
        Html.AddScriptParts(ResourceLocation.Footer, "~/vendor/plugins/magnific/jquery.magnific-popup.js", order: 999);
    }*@

@section CurPageScripts
{

    <script type="text/javascript">

        jQuery(document).ready(function () {

            "use strict";

            // Init Theme Core
            Core.init();

            // Init Demo JS
            Demo.init();

            //权限提示
            if ($(this).getUrlQueryString("Authorize") == "noAuthorize") {
                $('#alert-demo-2').fadeToggle();
                setTimeout(function () {
                    $('#alert-demo-2').fadeToggle();
                }, 3000);
            }
            //年份改选
            $('#year').change(function () {
                var year = $(this).val();
                var nowDate = new Date();
                var yearNow = nowDate.getFullYear();
                var monthNow = nowDate.getMonth() + 1;
                var beginMonth = 1;
                var endMonth = 12;
                if (year == 2021) {
                    beginMonth = 7;
                }
                if (year == yearNow) {
                    endMonth = monthNow;
                }
                $('#month').empty();
                for (var i = beginMonth; i <= endMonth; i++) {
                    $('#month').append("<option value='" + i + "'>" + i + "</option>");
                }
            });

            //异步获取业务员拜访明细
            var loadBusinessVisitOfYear = function (year, month) {
                //获取当前月天数
                var d = new Date(year, month, 0);
                var days = d.getDate();
                var colnums = [];
                colnums.push({ "field": "UserName", "title": "业务员", "width": 100 });
                for (var i = 1; i <= days; i++) {
                    colnums.push({
                        "field": "Days" + i, "title": i + "日", "width": 100, align: 'center', "formatter": function (value, row, index) {
                            if (value > 0) {
                                return '<span style="color:#FFFFFF;font-weight:bold;">' + value + '</span>';
                            }
                            return '<span>' + value + '</span>';
                        },
                        "cellStyle": function (value, row, index) {
                            if (value > 0) {
                                return { css: { "background-color": "red" } };
                            }
                            return '';
                        }
                    });
                }
                colnums.push({
                    "field": "Total", "title": "合计", "width": 100, align: 'center', "formatter": function (value, row, index) {
                        if (value > 0) {
                            return '<span style="font-weight:bold;">' + value + '</span>';
                        }
                        return '<span>' + value + '</span>';
                    }
                });
                //添加月份表格
                var strHtml = '';
                strHtml += '<div class="row">';
                strHtml += '<div class="col-md-12 admin-grid">';
                strHtml += '<div class="panel sort-disable">';
                strHtml += '<div class="panel-heading">';
                strHtml += '<span class="panel-title">' + year + '年' + month + '月业务员拜访量统计</span>';
                strHtml += '</div>';
                strHtml += '<div class="panel-body pn">';
                strHtml += '<div class="fixed-table-body">';
                strHtml += '<table id="businessUserVisit' + month + '" class="table table-striped table-bordered table-hover"></table>';
                strHtml += '</div>';
                strHtml += '</div>';
                strHtml += '</div>';
                strHtml += '<div style="width:100%;text-align:right;">';
                strHtml += '<a href="javascript:void(0);" onclick="exportDataOfMonth(' + year + ',' + month + ')" class="btn btn-info btn-sm mr5" style="margin-left:5px;"><span class="glyphicon glyphicon-export mr5" ></span >导出</a >';
                strHtml += '</div>';
                strHtml += '</div>';
                strHtml += '</div>';
                $('#panel1').html('');
                $('#panel1').append(strHtml);
                //载入明细
                $("#businessUserVisit" + month).bootstrapTable({
                    url: '/StaffReport/GetBusinessUserVisitOfYearList',
                    striped: true,
                    cache: false,
                    pagination: true,
                    showPagination: false,
                    sidePagination: "server",
                    queryParams: function (params) {
                        var temp =
                        {
                            year: year,
                            month: month
                        };
                        return temp;
                    },
                    sortable: false,
                    sortOrder: "asc",
                    search: false,
                    pageSize: 100,//每页的记录行数（*）
                    pageList: [100],
                    strictSearch: false,
                    showColumns: false,
                    showRefresh: false,
                    clickToSelect: false,
                    uniqueId: "id",
                    showToggle: false,
                    editable: true,
                    cardView: false,
                    detailView: false,
                    showFooter: false,
                    onLoadSuccess: function (data) {
                        
                    },
                    onPostBody: function (data) {
                        $("[data-toggle='popover']").popover();
                    },
                    columns: colnums
                });
            }
            var loadData = function () {
                var month = $('#month').val();
                var year = $('#year').val();
                loadBusinessVisitOfYear(year, month);
            }
            loadData();
            $('#month').change(function () {
                var month = $('#month').val();
                var year = $('#year').val();
                loadBusinessVisitOfYear(year, month);
            });
            //VisitExportAll
            //$("#VisitExportAll").click(function ()
            //{
            //    location.href = "/staffreport/visitexportall?type=" + $("#CType").val() + "&start=" + $("#StartTime").val() + "&end=" + $("#EndTime").val();
            //});
        });
        function exportDataOfMonth(year, month) {
            location.href = "/staffreport/ExportBusinessUserVisitOfYear?year=" + year + "&month=" + month;
        }
    </script>
}
